package Interfaz;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import Atxy2k.CustomTextField.RestrictedTextField;
import LogicaDeNegocio.GestionSistema;
import Modelo.Entidades.TipoDocumento;
import net.miginfocom.swing.MigLayout;

import javax.swing.JList;

import java.awt.ScrollPane;
import java.awt.Canvas;

import javax.swing.JEditorPane;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ImprimirLicencia extends JPanel {
	private GestionSistema admSistema = GestionSistema.getInstance();
	private Menu menu;

	private JPanel panelSuperior;
	private JPanel panelCentral;
	private JPanel panelAux;
	private JComboBox cbTipoDocumento;
	private JTextField textFieldNumeroDocumento;
	private RestrictedTextField restrictedNumeroDoc;
	private DefaultListModel modeloLista;
	private JList list;
	private JButton btnBuscar;

	/**
	 * Create the panel.
	 */
	public ImprimirLicencia() {
		setLayout(new BorderLayout());
		setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Imprimir Licencia", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setPreferredSize(new Dimension(602,500));
		
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		add(buttonPane,BorderLayout.SOUTH);
		
		JButton reversoButton = new JButton("Ver Reverso");
		reversoButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try {
					
				} catch (Throwable e) {
					new ErrorEstandar("");
				}
				
			}
		});
		reversoButton.setActionCommand("Reverso");
		reversoButton.setEnabled(false);
		buttonPane.add(reversoButton);
		
		JButton imprimirButton = new JButton("Imprimir");
		imprimirButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try {
				} catch (Throwable e) {
					new ErrorEstandar("");
				}
				
			}
		});
		imprimirButton.setActionCommand("Imprimir");
		imprimirButton.setEnabled(false);
		buttonPane.add(imprimirButton);
		
		panelSuperior = new JPanel();
		panelSuperior.setLayout(new MigLayout("", "[][][][grow][]", "[][]"));
		this.add(panelSuperior,BorderLayout.NORTH);
		
		JLabel lblTipoDocumento = new JLabel("Tipo documento");
		panelSuperior.add(lblTipoDocumento, "cell 0 0,alignx trailing");
		
		cbTipoDocumento = new JComboBox();
		panelSuperior.add(cbTipoDocumento, "cell 1 0,growx");
		cbTipoDocumento.setModel(new DefaultComboBoxModel(admSistema.getTipoDocumentos()));
		
		JScrollPane scrollPane = new JScrollPane();
		panelSuperior.add(scrollPane, "cell 3 0 2 2,growx");
		
		list = new JList();
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if(list.getSelectedIndex()!=-1){
					panelCentral.remove(panelAux);
					panelAux = new VistaFrontal();
					panelCentral.add(panelAux);
					menu.getInstance().setSize(new Dimension(602,600));
					//TODO llenar con los labels con los datos de la licencia
				}
			}
		});
		scrollPane.setViewportView(list);
		
		modeloLista = new DefaultListModel();
		list.setModel(modeloLista);
		
		JLabel lblNumeroDocumento = new JLabel("Numero documento");
		panelSuperior.add(lblNumeroDocumento, "cell 0 1,alignx trailing");
		
		textFieldNumeroDocumento = new JTextField();
		panelSuperior.add(textFieldNumeroDocumento, "cell 1 1,growx");
		textFieldNumeroDocumento.setColumns(10);
		
		restrictedNumeroDoc = new RestrictedTextField(textFieldNumeroDocumento);
		restrictedNumeroDoc.setLimit(8);
		restrictedNumeroDoc.setOnlyNums(true);
		
		btnBuscar = new JButton("Buscar");
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TipoDocumento tipoDoc = (TipoDocumento) cbTipoDocumento.getSelectedItem();
				String numeroDocumento = textFieldNumeroDocumento.getText();
				//TODO GABI/BRUNO hacer metodo para devolverme las licencias vigentes de un titular pasandole el titular
				//Titular titular = admTitular.getTitular(tipoDoc,numeroDocumento);
				//Vector<Licencia> licencias = admLicencia.getLicencia(titular);
				//TODO por cada licencia agregarlo al JList
				
			}
		});
		panelSuperior.add(btnBuscar, "cell 2 1");
		
		panelCentral = new JPanel();
		panelCentral.setLayout(new BorderLayout());
		panelCentral.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Vista preliminar", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		this.add(panelCentral,BorderLayout.CENTER);
		
		panelAux = new JPanel();
		panelCentral.add(panelAux,BorderLayout.CENTER);
		
	}
}
